package com.dragon.www.haier.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.entity.SingleReturnInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;
import com.dragon.www.haier.view.BaseItemView;

/**
 * Created by liqp on 2016/3/16.
 */
public class AirconditionAdapter extends AppArrayAdapter<Device, AirconditionAdapter.AirViewHolder> {

    public AirconditionAdapter(Context context) {
        super(context, R.layout.aircondition_view_item, AirViewHolder.class);
    }

    public void setAction(final Device air, int value, final String action) {
        NetHelper.sendAction(context, Integer.parseInt(air.getequipNo().split("-")[0]), 1, action, value, "aircondition", new NetUtils.NetCallBack<DeviceInfo>() {
            //            Integer.parseInt(air.getequipNo().split("-")[0])
            @Override
            public void success(DeviceInfo rspData) {
                if (rspData.getError() == 0) {
//                    final SingleReturnInfo SingleReturnInfo = (SingleReturnInfo) rspData;
//                    int value = SingleReturnInfo.getReturnInfo().getValue();
//                    if (action.equals("switch")){
//                        deviceInfo1[0].getAirValues().setSwitchValue(value);
//                    } else if (action.equals("temperatureadd")){
//                        deviceInfo1[0].getAirValues().setTemperature(value);
//                    } else if (action.equals("temperaturedel")){
//                        deviceInfo1[0].getAirValues().setTemperature(value);
//                    } else if (action.equals("mode")){
//                        deviceInfo1[0].getAirValues().setModeValue(value);
//                    } else if (action.equals("speed")){
//                        deviceInfo1[0].getAirValues().setSpeedValue(value);
//                    }
//                    notifyDataSetChanged();
                } else {
                    String description = rspData.getDescription();
                    Log.e("error", description);
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }, SingleReturnInfo.class);
    }

    /**
     * 临时对象绑定
     *
     * @param convertView
     * @param parent
     * @param position
     */
    @Override
    public View bindHolder(View convertView, ViewGroup parent, final int position) {
        final Device air = this.devices.get(position);
        AirViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new AirViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.aircondition_view_item, parent, false);
            viewHolder.airconditionDesc = (TextView) convertView.findViewById(R.id.tv_aircondition_desc);
            viewHolder.tvTem = (TextView) convertView.findViewById(R.id.tv_aircondition_tem);
            viewHolder.tvTemAdd = (TextView) convertView.findViewById(R.id.tv_tem_add);
            viewHolder.tvTemReduce = (TextView) convertView.findViewById(R.id.tv_tem_reduce);
            viewHolder.radioGroupMode = (RadioGroup) convertView.findViewById(R.id.radioGroup_wind_mode);
            viewHolder.radioGroupSpeed = (RadioGroup) convertView.findViewById(R.id.radioGroup_wind_speed);
            viewHolder.airconditionOnOff = (BaseItemView) convertView.findViewById(R.id.baseItemView_aicondition);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (AirViewHolder) convertView.getTag();
        }

        viewHolder.airconditionDesc.setText(air.getDeviceName());

        if (deviceInfo1 != null ){
            if ( Integer.parseInt(air.getequipNo().split("-")[0]) == box1) {
                viewHolder.tvTem.setText(deviceInfo1[0].getAirValues().getTemperature() + "");
                setChecked(viewHolder.radioGroupMode, deviceInfo1[0].getAirValues().getModeValue(), true);
                setChecked(viewHolder.radioGroupSpeed, deviceInfo1[0].getAirValues().getSpeedValue(), true);
                if (deviceInfo1[0].getAirValues().getSwitchValue() == 0) {
                    viewHolder.airconditionOnOff.turnOff();
                } else if (deviceInfo1[0].getAirValues().getSwitchValue() == 1){
                    viewHolder.airconditionOnOff.turnOn();
                }
            }else if( Integer.parseInt(air.getequipNo().split("-")[0]) == box2) {
                viewHolder.tvTem.setText(deviceInfo1[1].getAirValues().getTemperature() + "");
                setChecked(viewHolder.radioGroupMode,deviceInfo1[1].getAirValues().getModeValue() , true);
                setChecked(viewHolder.radioGroupSpeed, deviceInfo1[1].getAirValues().getSpeedValue(), true);
                if (deviceInfo1[1].getAirValues().getSwitchValue() == 0) {
                    viewHolder.airconditionOnOff.turnOff();
                } else if (deviceInfo1[1].getAirValues().getSwitchValue() == 1){
                    viewHolder.airconditionOnOff.turnOn();
                }
            }
        }

        viewHolder.airconditionOnOff.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (deviceInfo1 != null) {
                        if (Integer.parseInt(air.getequipNo().split("-")[0]) == box1) {
                            if (deviceInfo1[0].getAirValues().getSwitchValue() == 0) {
                                setAction(air, 1, "power");
                            } else if (deviceInfo1[0].getAirValues().getSwitchValue() == 1) {
                                setAction(air, 0, "power");
                            }
                        } else if (Integer.parseInt(air.getequipNo().split("-")[0]) == box2) {
                            if (deviceInfo1[1].getAirValues().getSwitchValue() == 0) {
                                setAction(air, 1, "power");
                            } else if (deviceInfo1[1].getAirValues().getSwitchValue() == 1) {
                                setAction(air, 0, "power");
                            }
                        }
                    }
                }
                return false;
            }
        });
        viewHolder.tvTemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAction(air,1,"temperatureadd");
            }
        });
        viewHolder.tvTemReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAction(air,1,"temperaturedel");
            }
        });
        viewHolder.radioGroupMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int id = group.getCheckedRadioButtonId();
                switch (id) {
                    case R.id.rb_mode_auto:
                        setAction(air,0,"mode");
                        break;
                    case R.id.rb_mode_airy:
                        setAction(air,1,"mode");
                        break;
                    case R.id.rb_mode_heat:
                        setAction(air,2,"mode");
                        break;
                    case R.id.rb_mode_cold:
                        setAction(air,3,"mode");
                        break;
                    case R.id.rb_mode_dehumidify:
                        setAction(air,4,"mode");
                        break;
                }

            }
        });
        viewHolder.radioGroupSpeed.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int id = group.getCheckedRadioButtonId();
                switch (id) {
                    case R.id.rb_speed_auto:
                        setAction(air,0,"speed");
                        break;
                    case R.id.rb_speed_low:
                        setAction(air,1,"speed");
                        break;
                    case R.id.rb_speed_mid:
                        setAction(air,2,"speed");
                        break;
                    case R.id.rb_speed_high:
                        setAction(air,3,"speed");
                        break;
                }
            }
        });

        return convertView;
    }

    public void setChecked(RadioGroup radioGroup, int checkedId, boolean flag) {
        if (flag) {
            for (int i = 0; i < radioGroup.getChildCount(); i++) {
                if (i == checkedId) {
                    radioGroup.getChildAt(i).setBackgroundResource(R.drawable.shape_state_checked_shadow);
                } else {
                    radioGroup.getChildAt(i).setBackgroundResource(R.drawable.shape_state_checked);
                }
            }
        }
    }

    public static class AirViewHolder extends AppHolder {
        TextView airconditionDesc;
        TextView tvTem;
        TextView tvTemAdd;
        TextView tvTemReduce;
        RadioGroup radioGroupMode;
        RadioGroup radioGroupSpeed;
        BaseItemView airconditionOnOff;
    }
}
